package dimension.starry.item.material;

import com.google.gson.stream.JsonToken;
import dimension.starry.item.ItemRegisterManager;
import dimension.starry.item.typeInterface.MaterialType;
import net.fabricmc.fabric.api.item.v1.FabricItemSettings;
import net.minecraft.advancement.criterion.Criteria;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.FluidDrainable;
import net.minecraft.block.FluidFillable;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.*;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundEvent;
import net.minecraft.stat.Stats;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.util.hit.BlockHitResult;
import net.minecraft.util.hit.HitResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.world.RaycastContext;
import net.minecraft.world.World;

/**
 * @author 150149
 */
public class StonePot extends MaterialType {

    public StonePot() {
        super(new FabricItemSettings());
    }

    @Override
    public TypedActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand) {
        ItemStack itemStack = playerEntity.getStackInHand(hand);
        if (!world.isClient()) {
            BlockHitResult blockHitResult = BucketItem.raycast(world, playerEntity,
                RaycastContext.FluidHandling.SOURCE_ONLY);
            if (blockHitResult.getType() == HitResult.Type.BLOCK) {
                BlockPos blockPos = blockHitResult.getBlockPos();
                if (!world.canPlayerModifyAt(playerEntity, blockPos)) {
                    return TypedActionResult.fail(itemStack);
                }
                System.out.println("aaaaa" + world.getBlockState(blockPos).getBlock().getTranslationKey());
                if (world.getBlockState(blockPos).getBlock().getTranslationKey().contains("water")) {
                    System.out.println("bbbbb");
                    itemStack = new ItemStack(ItemRegisterManager.WATER_STONE_POT.getItem(), itemStack.getCount());
                    world.setBlockState(blockPos, Blocks.AIR.getDefaultState(), 3);
                    return TypedActionResult.fail(itemStack);
                }
                return TypedActionResult.fail(itemStack);
            }
        }
        return TypedActionResult.pass(itemStack);
    }

}
